package com.dries.portal.module.friends.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.common.exception.ApiException;
import com.dries.portal.module.user.mapper.UserMapper;
import com.dries.portal.module.user.entity.UserEntity;
import com.dries.portal.module.friends.entity.Friends;
import com.dries.portal.module.friends.mapper.FriendsMapper;
import com.dries.portal.module.friends.service.IFriendsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends> implements IFriendsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserEntity> friends(String userId) {

        List<Long> userIds = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(Friends::getUserId, userId)
                .list()
                .stream().filter(Objects::nonNull)
                .map(Friends::getFriendId)
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(userIds)) {
            // 批量查找好友信息
            return new LambdaQueryChainWrapper<>(userMapper)
                    .in(UserEntity::getUserid, userIds)
                    .list();
        } else {
            throw new ApiException("暂无好友信息");
        }

    }

}